package com.zx.task.manager.service.impl;

import com.zx.core.enums.StatusEnum;
import com.zx.core.wrapper.ListWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.security.block.uitls.SecurityUtils;
import com.zx.task.manager.dao.TaskTypesMapper;
import com.zx.task.manager.dao.TasksMapper;
import com.zx.task.manager.mapstruct.TaskTypesMapStructMapper;
import com.zx.task.manager.model.TaskTypes;
import com.zx.task.manager.model.Tasks;
import com.zx.task.manager.param.TaskTypesParam;
import com.zx.task.manager.service.ITaskTypesService;
import com.zx.task.manager.utils.TaskNoUtils;
import com.zx.task.manager.vo.TaskTypesVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * description: 任务类型服务实现类 <br>
 * create: 2025-10-13 20:15
 * </p>
 *
 * @author zhou  xun
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TaskTypesServiceImpl implements ITaskTypesService {
    private final TaskTypesMapper taskTypesMapper;
    private final TasksMapper tasksMapper;

    @Override
    public ListWrapper<TaskTypesVO> getTaskTypesVOList(TaskTypesParam taskTypesParam) {
        ListWrapper<TaskTypesVO> result = new ListWrapper<>();
        try {
            // 创建任务类型对象
            TaskTypes taskTypes = TaskTypesMapStructMapper.INSTANCE.taskTypesParamToTaskTypes(taskTypesParam);
            // 查询任务类型列表
            List<TaskTypes> taskTypesList = taskTypesMapper.selectTaskTypesList(taskTypes);

            List<TaskTypesVO> taskTypesVOList = taskTypesList.stream()
                    .map(TaskTypesMapStructMapper.INSTANCE::taskTypesToTaskTypesVO)
                    .collect(Collectors.toList());

            result.setData(taskTypesVOList);
        } catch (Exception e) {
            log.error("查询任务类型列表时发生错误", e);
            result.fail("查询任务类型列表时发生错误: " + e.getMessage());
        }
        return result;
    }

    @Override
    public ResponseWrapper addTaskType(TaskTypesParam taskTypesParam) {
        ResponseWrapper result = new ResponseWrapper();
        try {
            // 验证任务类型状态
            StatusEnum statusEnum = StatusEnum.getEnumByCode(taskTypesParam.getStatus());
            if (null == statusEnum) {
                result.fail("传入的任务类型状态错误!");
                return result;
            }
            // 创建任务类型对象
            TaskTypes taskTypes = TaskTypesMapStructMapper.INSTANCE.taskTypesParamToTaskTypes(taskTypesParam);
            // 设置id
            taskTypes.setId(TaskNoUtils.generateTaskId());
            taskTypes.setCreateBy(SecurityUtils.getUserId());
            taskTypes.setCreateTime(LocalDateTime.now());
            // 插入任务类型
            taskTypesMapper.insertSelective(taskTypes);
        } catch (Exception e) {
            log.error("新增任务类型时发生错误", e);
            result.fail("新增任务类型时发生错误: " + e.getMessage());
        }
        return result;
    }

    @Override
    public ResponseWrapper editTaskType(TaskTypesParam taskTypesParam) {
        ResponseWrapper result = new ResponseWrapper();
        try {
            // 验证任务类型状态
            StatusEnum statusEnum = StatusEnum.getEnumByCode(taskTypesParam.getStatus());
            if (null == statusEnum) {
                result.fail("传入的任务类型状态错误!");
                return result;
            }
            // 验证任务类型是否存在
            TaskTypes selectByPrimaryKey = taskTypesMapper.selectByPrimaryKey(taskTypesParam.getId());
            if (null == selectByPrimaryKey) {
                result.fail("传入的任务类型信息不存在!");
                return result;
            }
            TaskTypes taskTypes = TaskTypesMapStructMapper.INSTANCE.taskTypesParamToTaskTypes(taskTypesParam);
            taskTypes.setUpdateBy(SecurityUtils.getUserId());
            taskTypes.setUpdateTime(LocalDateTime.now());
            // 更新任务类型
            taskTypesMapper.updateByPrimaryKeySelective(taskTypes);
        } catch (Exception e) {
            log.error("编辑任务类型时发生错误", e);
            result.fail("编辑任务类型时发生错误: " + e.getMessage());
        }
        return result;
    }

    @Override
    public ResponseWrapper updateTaskTypeStatus(TaskTypesParam taskTypesParam) {
        ResponseWrapper result = new ResponseWrapper();
        try {
            // 验证任务类型状态
            StatusEnum statusEnum = StatusEnum.getEnumByCode(taskTypesParam.getStatus());
            if (null == statusEnum) {
                result.fail("传入的任务类型状态错误!");
                return result;
            }
            // 验证任务类型是否存在
            TaskTypes selectByPrimaryKey = taskTypesMapper.selectByPrimaryKey(taskTypesParam.getId());
            if (null == selectByPrimaryKey) {
                result.fail("传入的任务类型信息不存在!");
                return result;
            }
            TaskTypes taskTypes = TaskTypesMapStructMapper.INSTANCE.taskTypesParamToTaskTypes(taskTypesParam);
            taskTypes.setUpdateBy(SecurityUtils.getUserId());
            taskTypes.setUpdateTime(LocalDateTime.now());
            // 更新任务类型
            taskTypesMapper.updateByPrimaryKeySelective(taskTypes);
        } catch (Exception e) {
            log.error("编辑任务类型状态时发生错误", e);
            result.fail("编辑任务类型状态时发生错误: " + e.getMessage());
        }
        return result;
    }

    @Override
    public ResponseWrapper deleteTaskType(String taskTypeId) {
        ResponseWrapper result = new ResponseWrapper();
        try {
            // 验证任务类型是否存在
            TaskTypes selectByPrimaryKey = taskTypesMapper.selectByPrimaryKey(taskTypeId);
            if (null == selectByPrimaryKey) {
                result.fail("传入的任务类型信息不存在!");
                return result;
            }
            // 验证任务类型下是否存在任务,调用任务信息列表根据类型ID查询任务列表,如果有则提示已经存在任务不能删除
            List<Tasks> tasksList = tasksMapper.selectTaskListByTaskTypeId(taskTypeId);
            if (!tasksList.isEmpty()) {
                result.fail("任务类型下存在任务,不能删除!");
                return result;
            }
            // 删除任务类型
            taskTypesMapper.deleteByPrimaryKey(taskTypeId);
        } catch (Exception e) {
            log.error("删除任务类型时发生错误", e);
            result.fail("删除任务类型时发生错误: " + e.getMessage());
        }
        return result;
    }
}

